package com.example.ming.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.example.ming.R;

public class VideoPlayActivity extends AppCompatActivity {

    private String videoPath;//本视频地址
    private int position;//传递是视频详情界面点击的视频位置
    private VideoView videoView;
    private MediaController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);

        videoPath = getIntent().getStringExtra("videoPath");
        position = getIntent().getIntExtra("position",0);
        init();

    }

    private void init() {
        videoView = (VideoView) findViewById(R.id.videoView);
        controller = new MediaController(this);
        videoView.setMediaController(controller);
        play();
    }

    private void play() {//播放视频
        if (TextUtils.isEmpty(videoPath)){
            Toast.makeText(this, "本地没有视频，暂时无法播放", Toast.LENGTH_SHORT).show();
            return;
        }
        int videoFileResourceId = getResources().getIdentifier(videoPath,"raw",getPackageName());
        String uri = "android.resource://" + getPackageName() + "/" + videoFileResourceId;
        videoView.setVideoPath(uri);
        videoView.start();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event){
        //把视频详情界面传递过来的被点击视频的位置传递回去
        Intent data = new Intent();
        data.putExtra("position",position);
        setResult(RESULT_OK, data);
        return super.onKeyDown(keyCode,event);
    }

}
